﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace GoogleMaps
{
	/// <summary>
	/// Specifies the images that is used to display a GMarker on the map.
	/// </summary>
	public class GIcon
	{
		/// <summary>
		/// Initializes a new instance of the GIcon class.
		/// </summary>
		public GIcon()
		{
			IsDefault = true;
		}

		/// <summary>
		/// Renders the control by appending it to the stringbuilder that contains the JavaScript 
		/// for the related Google Map.
		/// </summary>
		/// <param name="js">A string builder containing the javascript that goes with the Google Map.</param>
		public void Render(StringBuilder js)
		{
			js.AppendFormat(CultureInfo.InvariantCulture, "var {0} = new GIcon({1});", Id, IsDefault ? "G_DEFAULT_ICON" : "");
			
			if (!String.IsNullOrEmpty(ImageHref))
				js.AppendFormat(CultureInfo.InvariantCulture, "{0}.image = \"{1}\";", Id, ImageHref);

			if (!String.IsNullOrEmpty(ShadowHref))
				js.AppendFormat(CultureInfo.InvariantCulture, "{0}.shadow = \"{1}\";", Id, ShadowHref);

			if (IconSize != null)
				js.AppendFormat(CultureInfo.InvariantCulture, "{0}.iconSize = {1};", Id, IconSize.ToJavaScript());

			if (ShadowSize != null)
				js.AppendFormat(CultureInfo.InvariantCulture, "{0}.shadowSize = {1};", Id, ShadowSize.ToJavaScript());

			if (Anchor != null)
				js.AppendFormat(CultureInfo.InvariantCulture, "{0}.iconAnchor = {1};", Id, Anchor.ToJavaScript());

			if (InfoWindowAnchor != null)
				js.AppendFormat(CultureInfo.InvariantCulture, "{0}.infoWindowAnchor = {1};", Id, InfoWindowAnchor.ToJavaScript());

			if (InfoShadowAnchor != null)
				js.AppendFormat(CultureInfo.InvariantCulture, "{0}.infoShadowAnchor = {1};", Id, InfoShadowAnchor.ToJavaScript());
		}

		/// <summary>
		/// Gets or sets the id of the icon. The value of Id will be used as the javascript variable that
		/// can be used to reference the GIcon.
		/// </summary>
		public string Id { get; set; }

		/// <summary>
		/// Gets or sets a flag indicating that this is the default icon for a map.
		/// </summary>
		public bool IsDefault { get; set; }

		/// <summary>
		/// Gets or sets the href location of the image that is being placed on the map.
		/// </summary>
		public string ImageHref { get; set; }

		/// <summary>
		/// Gets or sets the href location of the image that is used to create a shadow for the marker.
		/// </summary>
		public string ShadowHref { get; set; }

		/// <summary>
		/// Gets or sets the size of the icon.
		/// </summary>
		public GSize IconSize { get; set; }

		/// <summary>
		/// Gets or sets the size of the shadown.
		/// </summary>
		public GSize ShadowSize { get; set; }

		/// <summary>
		/// Gets or sets the point that represets the pixel coordinates relative to the top left corner
		/// of the icon image at which this icon is anchored to the map.
		/// </summary>
		public GPoint Anchor { get; set; }

		/// <summary>
		/// Gets or sets the point that represents the pixel coordinate relative to the top left 
		/// corner of the icon image at which the info window is anchored to this icon.
		/// </summary>
		public GPoint InfoWindowAnchor { get; set; }

		/// <summary>
		/// Gets or sets the point that represets the pixel coordinates relative to the top left corner
		/// of the icon image at which this shadow is anchored to the map.
		/// </summary>
		public GPoint InfoShadowAnchor { get; set; }
	}
}
